package common;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class BMP2JPEG {

    public static String bmpToJpeg(String from) throws Exception {
        StringBuffer outPath = new StringBuffer("");

        File bmpfile = new File(from);
        if (!bmpfile.exists() || bmpfile.isDirectory()) {
            throw new RuntimeException("Error:文件不存在!");
        }

        BufferedImage im = ImageIO.read(bmpfile);
        File jpegfile = new File(bmpfile.getCanonicalPath().replaceAll(".bmp", ".jpg"));
        ImageIO.write(im, "jpg", jpegfile);

        outPath.append(jpegfile.getCanonicalPath());
        outPath.append(";");
        return outPath.toString();
    }

    public static String bmpToJpeg(String from, int angle) throws Exception {
        StringBuffer outPath = new StringBuffer("");

        File bmpfile = new File(from);
        if (!bmpfile.exists() || bmpfile.isDirectory()) {
            throw new RuntimeException("Error:文件不存在!");
        }

        BufferedImage im = ImageIO.read(bmpfile);

        File jpegfile = new File(bmpfile.getCanonicalPath().replaceAll(".bmp", ".jpg"));

        ImageIO.write(im, "jpg", jpegfile);

        // 旋转处理
        PicUtil pic = new PicUtil(jpegfile);
        pic.rotate(angle);
        pic.createPic(jpegfile);

        outPath.append(jpegfile.getCanonicalPath());
        outPath.append(";");
        return outPath.toString();
    }
}
